#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>

float poisson( float rate ){
	return -logf(1.0-(float)rand()/RAND_MAX) / rate;
}

float exponential( float average ){
	float u = (float)rand()/RAND_MAX;	
	return -logf( u )*average;
}

int uniform( int min, int max ){
	return (rand()%(max-min+1))+min;
}

int main( int argc, char** argv ){
	srand( time( 0 ) );
	
	int number_of_VNs;
	float arrival_rate;
	int average_lifetime;
	int schedule_window;
	int min_windows_wait;
	int max_windows_wait;
	
	sscanf( argv[1], "%d", &number_of_VNs );
	sscanf( argv[2], "%f", &arrival_rate );
	sscanf( argv[3], "%d", &average_lifetime );
	sscanf( argv[4], "%d", &schedule_window );
	sscanf( argv[5], "%d", &min_windows_wait );
	sscanf( argv[6], "%d", &max_windows_wait );
	
	FILE *out;
	
	out = fopen( "events/events.evt", "w" );
	if( !out ){
		printf( "Error in file generate_events.cpp, function main: Could not open file events/events.evt\n" );
		exit(-1);
	}
	
	int clock = 0;
	int last_event_time = 0;
	
	//this code generates the arrive departure and expire network events
	for( int i = 0; i< number_of_VNs; ++i ){
		int arrival_time = poisson( arrival_rate );
		int life_time = exponential( average_lifetime );
		
		clock = clock + arrival_time;
		
		int expire_time = ( clock-( clock % schedule_window )) + schedule_window * uniform( min_windows_wait, max_windows_wait );
		
		int departure_time = clock + life_time;
		
		last_event_time = ( last_event_time > expire_time ) ? last_event_time : clock;
		
		fprintf( out, "%d A %d\n", i, clock );
		fprintf( out, "%d D %d\n", i, departure_time );
		fprintf( out, "%d E %d\n", i, expire_time );
	}
	//this code generates the schedule events
	last_event_time = last_event_time + schedule_window;
	for( int i = schedule_window; i < last_event_time; i += schedule_window )
		fprintf( out,  "-1 S %d\n", i );
	
	fclose( out);
}